const express = require('express')
const cors = require("cors")
const app = new express()

app.use(cors())
app.use(express.json())

const db = require("./db")
const utiles = require("./utils")

const jwt = require('jsonwebtoken')

app.get('/post/list', (req, res) => {
  res.status(200).json({
    "code": 20000,
    "data": {
      "total": 101,
      "items": [
        {
          "id": 0,
          "isTop": true,
          "banner": "https://s1.ax1x.com/2020/05/14/YDhagx.jpg",
          "isHot": true,
          "pubTime": 1633024800000,
          "title": "看一遍闭着眼都会安装Lua了",
          "summary": "Lua 是一种轻量小巧的脚本语言，能为应用程序提供灵活的扩展和定制功能。",
          "content": "<p>I am testing data, I am testing data.</p><p><img src=\"https://wpimg.wallstcn.com/4c69009c-0fd4-4153-b112-6cb53d1cf943\"></p>",
          "viewsCount": 4045,
          "commentsCount": 99
        },
        {
          "id": 1,
          "isTop": false,
          "banner": "https://s1.ax1x.com/2020/05/14/YDhU81.jpg",
          "isHot": true,
          "pubTime": 1633024900000,
          "title": "深入浅出JavaScript",
          "summary": "JavaScript 是网页开发必不可少的语言。",
          "content": "<p>I am testing data, I am testing data.</p><p><img src=\"https://wpimg.wallstcn.com/4c69009c-0fd4-4153-b112-6cb53d1cf943\"></p>",
          "viewsCount": 2500,
          "commentsCount": 50
        },
        {
          "id": 2,
          "isTop": false,
          "banner": "https://s1.ax1x.com/2020/05/14/YDhBDO.jpg",
          "isHot": false,
          "pubTime": 1633025000000,
          "title": "Python 编程入门",
          "summary": "Python 是一门非常适合初学者的编程语言。",
          "content": "<p>I am testing data, I am testing data.</p><p><img src=\"https://wpimg.wallstcn.com/4c69009c-0fd4-4153-b112-6cb53d1cf943\"></p>",
          "viewsCount": 1800,
          "commentsCount": 20
        }
      ],
      "hasNextPage": false,
      "page": 1,
      "size": 10
    }
  }
  )
})
let category = 0
app.get('/category', (req, res) => {
  console.log(utiles.reqtime() + ":" + "category" + ":" + ++category)
  const sql = "SELECT * FROM category"
  db.query(sql, (err, results) => {
    if (err) { return res.status(500).json({ code: 50000, msg: "category err...", err }) }
    res.status(200).json({ code: 20000, data: results })
  })
})
let friend = 0
app.get('/friend', (req, res) => {
  console.log(utiles.reqtime() + ":" + "friend" + ":" + ++friend)
  db.query('SELECT * FROM friend', (error, results) => {
    if (error) {
      return res.status(500).json({ code: 50000, message: '数据库查询失败', error });
    }
    res.status(200).json({ code: 20000, data: results });
  });
})
// app.get('/social', (req, res) => {
//   res.status(200).json({
//     code: 20000,
//     data: [
//       {
//         id: 1,
//         title: 'QQ',
//         icon: 'iconqq',
//         color: '#1AB6FF ',
//         href: 'http://rblog.asia'
//       },
//       {
//         id: 2,
//         title: 'Gitee',
//         icon: 'icongitee',
//         color: '#d81e06',
//         href: 'http://rblog.asia'
//       },
//       {
//         id: 3,
//         title: 'GitHub',
//         icon: 'icongithub',
//         color: '',
//         href: 'http://rblog.asia'
//       },
//       {
//         id: 4,
//         title: 'CSDN',
//         icon: 'iconcsdn',
//         color: 'red',
//         href: 'http://rblog.asia'
//       }
//     ]
//   })
// })
let site = 0
app.get('/site', (req, res) => {
  console.log(utiles.reqtime() + ":" + "site" + ":" + ++site)
  const sql = "SELECT * FROM site"
  db.query(sql, (err, results) => {
    if (err) { return res.status(500).json({ code: 50000, msg: 'no!!!!', err }) }
    res.status(200).json({ code: 20000, data: results[0] })
  })
})
app.get('/focus/list', (req, res) => {
  res.status(200).json({
    code: 20000,
    data: [
      {
        id: 1,
        title: 'Akina',
        img: 'https://s1.ax1x.com/2020/05/14/YDfRnU.jpg'
      },
      {
        id: 2,
        title: '使用说明',
        img: 'https://s1.ax1x.com/2020/05/14/YDf4AJ.jpg'
      },
      {
        id: 3,
        title: '文章归档',
        img: 'https://s1.ax1x.com/2020/05/14/YDfT91.jpg'
      }
    ]
  })
})
app.get('/comment', (req, res) => {
  res.status(200).json(
    {
      "code": 20000,
      "data": [
        {
          "comment": {
            "id": 1,
            "postId": "",
            "parentId": "",
            "fromUserId": "1",
            "fromUserName": "张伟",
            "fromUserAvatar": "https://s1.ax1x.com/2020/05/14/YDhagx.jpg",
            "content": "这是一个评论的内容示例。",
            "createTime": 1633024800000,
            "toUserId": "",
            "toUserName": "李四",
            "toUserAvatar": "https://s1.ax1x.com/2020/05/14/YDhU81.jpg"
          },
          "reply": [
            {
              "id": 2,
              "postId": "",
              "parentId": "1",
              "fromUserId": "2",
              "fromUserName": "王小明",
              "fromUserAvatar": "https://s1.ax1x.com/2020/05/14/YDhBDO.jpg",
              "content": "这是回复评论的内容示例。",
              "createTime": 1633024900000,
              "toUserId": "1",
              "toUserName": "张伟",
              "toUserAvatar": "https://s1.ax1x.com/2020/05/14/YDhoVg.jpg"
            }
          ]
        }
      ]
    })
})

const userRouter = require('./router/user')
app.use('/user', userRouter)

// 全局错误捕捉器
app.use((err, req, res, next) => {
  console.error('未处理的错误:', err);
  res.status(500).send('服务器错误');
});
app.listen(1104, () => {
  console.log('1104running...')
})